package com.atguigu.redis;

import com.atguigu.constant.RedisConst;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class RedisChannelConfig {
    //用那个类的那个方法来处理接受到的消息  RedisMessageReceiver 这里的这个类是自定义的一个类
    @Bean
    MessageListenerAdapter listenerAdapter(RedisMessageReceiver receiver){
        return new MessageListenerAdapter(receiver,"receiveChannelMessage");
    }
    /*
    RedisMessageListenerContainer相当于一个listener
    MessageListenerAdapter  当有消息的时候接受并处理消息的实现类
    * */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);//设置链接
        //订阅哪个主题  这里就是订阅PREPARE_PUB_SUB_SECKILL
        container.addMessageListener(messageListenerAdapter,new PatternTopic(RedisConst.PREPARE_PUB_SUB_SECKILL));
        return container;
    }
}
